where $D_{0}$ is dividend at time $0$, $g$ is the dividend growth rate (use historical average) and $k=r_{f}+\beta[E(r_{m}-r_{f}]$ Discuss you results.
Compare your prediction with the real return and discuss your result
library(quantmod)
library(ggplot2)
# MSFT and SPY
getSymbols(c("MSFT", "SPY"), from = Sys.Date() - 10*365, to = Sys.Date(), src = "yahoo")
# DİV MSFT and SPY
div_MSFT = getDividends("MSFT", from = Sys.Date() - 10*365, to = Sys.Date())
div_SPY = getDividends("SPY", from = Sys.Date() - 10*365, to = Sys.Date())
# TNX
getSymbols("^TNX", from = Sys.Date() - 10*365, to = Sys.Date(), src = "yahoo")
# Convert to monthly
MSFT = to.monthly(MSFT)
SPY = to.monthly(SPY)
div_MSFT = to.monthly(div_MSFT)
div_SPY = to.monthly(div_SPY)
TNX = to.monthly(TNX)
#na.omit(SPY_returns)
#na.omit(MSFT_returns)
#na.omit(TNX_returns)
returns_MSFT = monthlyReturn(MSFT)
returns_SPY = monthlyReturn(SPY)
# average monthly risk-free rate
risk_free_rate = mean(TNX[,"TNX.Close"] / 1200)
ex_post_risk_premium_MSFT = colMeans(returns_MSFT) - risk_free_rate
ex_post_risk_premium_SPY = colMeans(returns_SPY) - risk_free_rate
cat("Ex-post equity risk premium for MSFT:", round(100*ex_post_risk_premium_MSFT, 2), "%\n")
cat("Ex-post equity risk premium for SPY:", round(100*ex_post_risk_premium_SPY, 2), "%\n")
Ex-post equity risk premium for MSFT: 1.93 % Ex-post equity risk premium for SPY: 0.71 %
results = data.frame(
Equity = c("MSFT", "SPY"),
Risk_Premium = c(ex_post_risk_premium_MSFT, ex_post_risk_premium_SPY)
)
# Plot
ggplot(results, aes(x = Equity, y = Risk_Premium)) +
geom_bar(stat = "identity", fill = "red") +
ggtitle("Ex-post Equity Risk Premium") +
xlab("Equity") +
ylab("Risk Premium") +
theme_minimal()
div_growth_MSFT = diff(log(div_MSFT)) * 12
div_growth_SPY = diff(log(div_SPY)) * 12
avg_div_growth_SPY = mean(div_growth_SPY, na.rm = TRUE)
avg_div_growth_MSFT = mean(div_growth_MSFT, na.rm = TRUE)
avg_div_growth_SPY
avg_div_growth_MSFT
#str(div_MSFT)
avg_div_MSFT = mean(na.omit(div_MSFT[,"div_MSFT.Close"]))
#div_SPY
avg_div_SPY <- mean(na.omit(div_SPY[,"div_SPY.Close"]))
cat("Average MSFT:", avg_div_MSFT, "\n")
cat("Average SPY:", avg_div_SPY, "\n")
Average MSFT: 0.4365 Average SPY: 1.2498
where $D_{0}$ is dividend at time $0$, $g$ is the dividend growth rate (use historical average) and $k=r_{f}+\beta[E(r_{m}-r_{f}]$ Discuss you results.
# Calculate excess returns for MSFT and SPY
risk_free_rate = 0.03337
excess_MSFT = monthlyReturn(MSFT[,"MSFT.Adjusted"]) - risk_free_rate
excess_SPY = monthlyReturn(SPY[,"SPY.Close"]) - risk_free_rate
beta_MSFT = cov(excess_MSFT, excess_SPY) / var(excess_SPY)
# as.numeric(beta_MSFT)
exp_return_SPY <- mean(excess_SPY) + risk_free_rate
exp_return_MSFT <- risk_free_rate + beta_MSFT * (exp_return_SPY - risk_free_rate)
k <- risk_free_rate + beta_MSFT * (exp_return_SPY - risk_free_rate)
#class(div_MSFT)
risk_premium <- mean(excess_SPY)
risk_premium_spy = 0.031
current_dividend <- tail(div_MSFT[,4], 1)
First_dividend <- head(div_MSFT[,4], 1)
Years = 40
Expected_Growth_Rate = (as.numeric(current_dividend) / as.numeric(First_dividend)) ^ (1 / Years) - 1
Cost_equity = (risk_free_rate+beta_MSFT)*risk_premium_spy
Gordon_growth <- (as.numeric(current_dividend) * (1 + Expected_Growth_Rate)) / (Cost_equity - Expected_Growth_Rate)
as.numeric(Gordon_growth)
# PAYOUT RATİO MSFT
library(httr)
library(jsonlite)
# Set API key and endpoint
api_key <- "6da598f1d7656cc76902b839cb06ce7a"
endpoint <- "https://financialmodelingprep.com/api/v3"
# Send request to get financial ratios for MSFT
res <- GET(paste0(endpoint, "/ratios-ttm/MSFT?apikey=", api_key))
# Convert response to JSON
content <- content(res, "text")
json_data <- fromJSON(content)
payout_ratio = json_data$payoutRatioTTM
payout_ratio
0.281160580586814
Compare your prediction with the real return and discuss your result
# Set start and end dates for the 9-year period
start_date <- as.Date("2012-04-05")
end_date <- as.Date("2022-04-04")
getSymbols("MSFT", from = start_date, to = end_date, src = "yahoo")
# monthly adjusted prices and dividend amounts
monthly_prices <- to.monthly(MSFT)[,4]
monthly_dividends <- monthlyReturn(MSFT[,6], type = "log") * 100
# monthly prices and dividends into a data frame
msft_df <- data.frame(date = index(monthly_prices),
price_close = as.numeric(monthly_prices),
dividends = as.numeric(monthly_dividends))
# date column to year-month format
msft_df$date <- format(as.Date(msft_df$date), "%Y-%m")
# the dividend price ratio
msft_df$dividend_price_ratio <- msft_df$dividends / msft_df$price_close
# Subset the data for the first 9 years
msft_9 <- msft_df[1:108,]
# Fit the Shiller model summarize results
shiller_model <- lm(msft_9$price_close ~ msft_9$dividend_price_ratio)
summary(shiller_model)
Call:
lm(formula = msft_9$price_close ~ msft_9$dividend_price_ratio)
Residuals:
Min 1Q Median 3Q Max
-60.88 -43.09 -25.21 28.00 151.00
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 84.841 5.767 14.710 <2e-16 ***
msft_9$dividend_price_ratio -11.523 48.958 -0.235 0.814
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 58.46 on 106 degrees of freedom
Multiple R-squared: 0.0005224, Adjusted R-squared: -0.008907
F-statistic: 0.0554 on 1 and 106 DF, p-value: 0.8144
# Calculate predicted returns based on the Shiller model
predicted_returns <- predict(shiller_model)
# Plot predicted returns against actual returns
plot(msft_9$price_close, predicted_returns, xlab = "Actual Returns", ylab = "Predicted Returns")
Based on the our output of the summary(shiller_model) command, it appears that for the MSFT stock, the coefficient of the dividend price ratio is not statistically different from 0. This means that the Shiller model does not provide a significant relationship between the dividend price ratio and future stock returns for MSFT over the 9-year period from 2012 to 2021.
Additionally,ı think the $R-squared$ value of the model is very low, which indicates that the model does not explain much of the variation in the stock returns. Therefore, based on the Shiller model, the dividend price ratio does not appear to be a reliable indicator of future stock returns for $MSFT$.